package com.example.skyline.controller;

import com.example.skyline.common.Result;
import com.example.skyline.entity.Menu;
import com.example.skyline.entity.Role;
import com.example.skyline.entity.User;
import com.example.skyline.entity.UserRole;
import com.example.skyline.service.MenuService;
import com.example.skyline.service.RoleService;
import com.example.skyline.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @Resource
    private UserService userService;

    // 返回角色对应的菜单列表 ---> 所有菜单列表
    @GetMapping("/fetchMenusForRole/{userId}")
    public Result fetchMenusForRole(@PathVariable("userId") Integer userId) {
        List<Menu> menus = menuService.fetchMenusForRole(userId);
        return Result.success(menus);
    }

    // 返回角色对应的菜单列表 ---> 一级菜单列表
    @GetMapping("/fetchMenusForRole")
    public Result fetchMenusForRole() {
        List<Role> list = roleService.fetchMenusForRoleAll();
        return Result.success(list);
    }

    // 通过一级菜的id获取二级菜单列表 ---> 通过父级菜单 获取子集菜单  [parentId ---> 父级菜单的id]
    @GetMapping("/fetchAllMenus")
    public Result fetchAllMenus(@RequestParam("parentId") Integer parentId) {
        List<Menu> menus = menuService.fetchAllMenus(parentId);
        return Result.success(menus);
    }

    // 查询所有一级菜单
    @GetMapping("/fetchAllParentMenus")
    public Result fetchAllParentMenus() {
        List<Menu> menus = menuService.fetchAllParentMenus();
        return Result.success(menus);
    }

    // 根据不同的角色添加菜单权限
    @PostMapping("/editRoleMenu")
    public Result addMenu(@RequestParam( value = "roleId", required = true) Integer roleId, @RequestParam( value = "menuRole", required = true) List<Integer> menuRole) {
        roleService.editRoleMenu(roleId, menuRole);
        return Result.success();
    }

    // 添加角色
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role) {
        Integer row = roleService.addRole(role);
        if(row == 0) {
            return Result.error("角色名称不能为空");
        }

        if(row == -1) {
            return Result.error("角色名称已存在");
        }

        return Result.success();
    }

    // 用户信息
    @GetMapping("/getUserInfo")
    public Result getRoleById(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "8") Integer pageSize,
                              @RequestParam(defaultValue = "") String name,
                              @RequestParam(defaultValue = "") String username) {
        PageInfo<User> pageInfo = userService.getUserInfo(pageNum, pageSize, name, username);
        return Result.success(pageInfo);
    }

    @GetMapping("/getAllRole")
    public Result getAllRole() {
        List<Role> list = roleService.fetchRoleAll();
        return Result.success(list);
    }

    @PutMapping("/editUserRole")
    public Result editUserRole(@RequestBody UserRole userRole) {
        int row = userService.editUserRole(userRole);
        if(row == 0) {
            return Result.error("编辑错误");
        }
        return Result.success();
    }

}
